﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yang.DataBase;

public partial class WebServices_getGpsRoute : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        string resultJson = "{\"Points\":[";
        int nPoints = 0;
        string machineCode = Request.QueryString[0].ToString();
        string startDate = Request.QueryString[1].ToString();
        string startHour = Request.QueryString[2].ToString();
        string startMinute = Request.QueryString[3].ToString();
        string endDate = Request.QueryString[4].ToString();
        string endHour = Request.QueryString[5].ToString();
        string endMinute = Request.QueryString[6].ToString();

        if (startDate.IndexOf("年") > 0)
        {
            startDate = startDate.Replace("年", "-");
            startDate = startDate.Replace("月", "-");
            startDate = startDate.Replace("日", "");

            endDate = endDate.Replace("年", "-");
            endDate = endDate.Replace("月", "-");
            endDate = endDate.Replace("日", "");
        }



        string startTime = startDate + " " + startHour + ":" + startMinute + ":00";
        string endTime = endDate + " " + endHour + ":" + endMinute + ":00";

        try
        {
            int step = 1;//GPS点位的间隔，由于设备每1秒记录一次GPS位置，而将全部点绘制出来，则会数据量非常大，影响客户度绘制速度。
            string today = DateTime.Now.ToShortDateString();

            string strSql = "select Latitude,Longitude,Memo from GpsInfo " +
                            "where DeviceCode = '" + machineCode + "' and " +
                            "LocationTime >= '" + startTime + "' and " +
                            "LocationTime <= '" + endTime + "' " +
                            "order by LocationTime";
            
            DataSet ds = Database.Query(strSql);
            nPoints = ds.Tables[0].Rows.Count;
            if (nPoints > 0)
            {
                step = (1 +(int)Math.Floor(nPoints / 100.0));

                for (int i = 0; i < nPoints; i = i + step)
                {
                    resultJson += "{\"MachineId\":\"" + machineCode + "\",\"lat\":\"" + ds.Tables[0].Rows[i][0].ToString() + "\",\"lon\":\"" + ds.Tables[0].Rows[i][1].ToString() + "\"},";
                }

                resultJson = resultJson.Substring(0, resultJson.Length - 1);
                resultJson += "]}";
            }
            else
            {
                Log.WriteInfo("农机没有启动GPS或处于停机状态。");
                resultJson = "{ \"Points\":\"0\"}";
            }

        }
        catch (Exception ex)
        {
            Log.WriteException(DateTime.Now.ToString() + ":" + ex.Message + "\n");
            resultJson = "{ \"Points\":\"0\"}";
        }
        Response.Write(resultJson);
    }
}
